package org.fuyd.algorithm.demo;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

/**
 * 该类主要演示定期执行某一事件
 * 
 * @author 永德
 *
 */
public class BeeperControl {
  private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

  public void beepForAnHour() {
    final Runnable beeper = new Runnable() {
      public void run() {
        System.out.println("beep");
      }
    };
    final ScheduledFuture<?> beeperHandle = scheduler.scheduleAtFixedRate(beeper, 10, 10, TimeUnit.SECONDS);
    scheduler.schedule(new Runnable() {
      public void run() {
        beeperHandle.cancel(true);
      }
    }, 60 * 60, TimeUnit.SECONDS);
  }
}
